//
//  YZPersonalVM.m
//  VET
//
//  Created by 刘文靖 on 2018/7/4.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZPersonalVM.h"
#import "YZPersonalAPI.h"
#import "YZSignAPI.h"
#import "YZUser.h"
#import "MJExtension.h"
#import "YZUploadAPI.h"
@interface YZPersonalVM()
@property (nonatomic, strong) YZSignAPI *inputUserInfoAPI;
@end
@implementation YZPersonalVM

/** =========个人资料修改=========== */
/** 头像修改 */
- (RACSignal*)uploadSignal:(UIImage*)image{
    // 上传图片
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[YZUploadAPI uploadImage:image imgPath:@"headImg" apiKey:@""] subscribeNext:^(NSDictionary *json) {
            
            [subscriber sendNext:json];
            
        } error:^(NSError * _Nullable error) {
            
            [subscriber sendError:error];
            [subscriber sendCompleted];
        } completed:^{
            
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}
/** 修改名字*/
- (RACSignal*)updteUserInfoWithName:(NSString *)name{
    [self.inputUserInfoAPI addParam:@"name" value:name];
    [self.inputUserInfoAPI addParam:@"nickname" value:name];
    @weakify(self)
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self)
        [[self.inputUserInfoAPI putRequest] subscribeNext:^(NSDictionary *data) {
            
            [[YZUser sharedInstance] storeUserToLocal:data];
            [subscriber sendNext:data];
            [subscriber sendCompleted];
            
        } error:^(NSError * _Nullable error) {
            
            [subscriber sendError:error];
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

/** 消息通知设置*/
- (RACSignal*)updteNotificationSetting:(NSDictionary *)dict{
    [self.inputUserInfoAPI addParams:dict];
    @weakify(self)
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self)
        [[self.inputUserInfoAPI putRequest] subscribeNext:^(NSDictionary *data) {
            
            [[YZUser sharedInstance] storeUserToLocal:data];
            [subscriber sendNext:data];
            [subscriber sendCompleted];
            
        } error:^(NSError * _Nullable error) {
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

/** 提交专业认证证书 */
- (RACSignal*)uploadCertificate:(UIImage*)image{
    // 上传图片
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[YZPersonalAPI uploadCertificate:image] subscribeNext:^(NSDictionary *json) {
            
            [subscriber sendNext:json];
            
        } error:^(NSError * _Nullable error) {
            
            [subscriber sendError:error];
            [subscriber sendCompleted];
        } completed:^{
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

/**兽医个人基本信息 */
- (RACSignal*)requestPersonalBaseInfo{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[YZPersonalAPI requestPersonalBaseInfo] subscribeNext:^(id  _Nullable x) {
            [[YZUser sharedInstance] storeUserToLocal:x];
            [subscriber sendNext:x];
            [subscriber sendCompleted];
            
        } error:^(NSError * _Nullable error) {
            
            [subscriber sendError:error];
            [subscriber sendCompleted];
        } completed:^{
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

/**修改密码,获取短信验证码 */
- (RACSignal*)requestMessageForPWD:(NSString *)mobile{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[YZPersonalAPI requestMessageForPWD:mobile] subscribeNext:^(id  _Nullable x) {
            
            [subscriber sendNext:x];
            
        } error:^(NSError * _Nullable error) {
            
            [subscriber sendError:error];
            [subscriber sendCompleted];
        } completed:^{
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}
/**修改密码,需要传入验证码,手机号,密码(此处传明文) */
- (RACSignal*)modifyPassword:(NSString *)newPWD mobile:(NSString *)mobile verCode:(NSString *)verCode{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[YZPersonalAPI modifyPassword:newPWD mobile:mobile verCode:verCode] subscribeNext:^(id  _Nullable x) {
            
            [subscriber sendNext:x];
            
        } error:^(NSError * _Nullable error) {
            
            [subscriber sendError:error];
            [subscriber sendCompleted];
        } completed:^{
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];}

/**兽医职业类型,返回String集合*/
- (RACSignal*)requestProfessionTypeArr{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[YZPersonalAPI requestProfessionTypeArr] subscribeNext:^(NSArray *data) {
            
            [subscriber sendNext:data];
            
        } error:^(NSError * _Nullable error) {
            
            [subscriber sendError:error];
            [subscriber sendCompleted];
        } completed:^{
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

- (RACSignal*)submitFeedback:(NSString *)feedbackType content:(NSString *)content{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        [[YZPersonalAPI submitFeedback:feedbackType content:content] subscribeNext:^(id  _Nullable x) {
            
            [subscriber sendNext:x];
            
        } error:^(NSError * _Nullable error) {
            
            [subscriber sendError:error];
            [subscriber sendCompleted];
        } completed:^{
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

- (RACSignal*)updteUserInfoWithProfessionType:(NSString *)professionType company:(NSString *)company          province:(NSString *)province city:(NSString *)city{
    [self.inputUserInfoAPI addParam:@"professionType" value:kText(professionType)];
    [self.inputUserInfoAPI addParam:@"company" value:kText(company)];
    [self.inputUserInfoAPI addParam:@"province" value:kText(province)];
    [self.inputUserInfoAPI addParam:@"city" value:kText(city)];
    @weakify(self)
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        @strongify(self)
        [[self.inputUserInfoAPI putRequest] subscribeNext:^(NSDictionary *data) {
            
            [[YZUser sharedInstance] storeUserToLocal:data];
            [subscriber sendNext:data];
            [subscriber sendCompleted];
            
        } error:^(NSError * _Nullable error) {
            
            [subscriber sendError:error];
            [subscriber sendCompleted];
        }];
        
        return [RACDisposable disposableWithBlock:^{}];
    }];
}

- (YZSignAPI *)inputUserInfoAPI {
    
    if (_inputUserInfoAPI == nil) {
        
        _inputUserInfoAPI = [YZSignAPI inputUserInfoAPI];
    }
    
    return _inputUserInfoAPI;
}
@end
